/**
 * Created by gongxun on 16/12/16.
 */

var KwxReplayLayer = GameCCSBase.extend({
    mSpeed : null,
    mSpeedType : null,
    mSpeedTemp : null,
    mIsPlay : null,
    mSpeedTypes : null,

    Text_replaySpeed : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxReplayLayer.json");
        this.mSpeed = 3.0;
        this.mSpeedTypeIndex = 1;
        this.mSpeedTypes = [1, 2, 4, 6, 8];
        this.mIsPlay = false;
        this.Text_replaySpeed = mo.seekWidgetByName(this.ccsNode, "Text_replaySpeed");

        mo.seekWidgetByName(this.ccsNode, "Button_replayExit").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.onPlayEnd();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_replayLast").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.onPlayLast();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_replayPlay").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (this.mIsPlay){
                    this.onStop();
                }else{
                    this.onStart();
                }
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_replayNext").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.onPlayNext();
            }
        }, this);

        //分享录像
        mo.seekWidgetByName(this.ccsNode, "Button_replayIdShare").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.kwxTable.onShareReplay();
            }
        }, this);

        this.updateSpeedText();
    },

    setReplayId : function(replayId){
        // mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_replayId"), "分享录像(" + replayId.substr(7) + ")");
    },

    updateReplay : function(dt){
        this.mSpeedTemp += dt;
        if (this.mSpeedTemp > this.getSpeed()){
            //播放
            this.mSpeedTemp = 0;
            var isPlay = mo.kwxTableStatus.playReplay();
            if (isPlay){
                //播放了一次
            }else{
                //播放结束
                this.onStop();
            }
        }
    },

    onStart : function(){
        this.mIsPlay = true;
        this.mSpeedTemp = 0;

        this.unschedule(this.updateReplay);
        this.schedule(this.updateReplay);

        this.updateSpeedText();
    },

    onStop : function(){
        this.mIsPlay = false;
        this.unschedule(this.updateReplay);

        this.updateSpeedText();
    },

    onPlayEnd : function(){
        this.onStop();
        mo.sceneHelper.intoHallLayer();
    },

    updateSpeedText : function(){
        var speed =  this.mSpeedTypes[this.mSpeedTypeIndex];
        mo.gameHelper.setTextString(this.Text_replaySpeed, speed + "X");

        if (this.mIsPlay){
            mo.seekWidgetByName(this.ccsNode, "Button_replayPlay").loadTextures("replay_pause_searchpai.png", "replay_pause_searchpai.png", "", ccui.Widget.PLIST_TEXTURE);
        }else{
            mo.seekWidgetByName(this.ccsNode, "Button_replayPlay").loadTextures("replay_play_searchpai.png", "replay_play_searchpai.png", "", ccui.Widget.PLIST_TEXTURE);
        }

    },

    onPlayNext : function(){
        var index = this.mSpeedTypeIndex + 1;
        if (index < this.mSpeedTypes.length){
            this.mSpeedTypeIndex = index;
        }else{

        }

        this.updateSpeedText();
    },

    onPlayLast : function(){
        var index = this.mSpeedTypeIndex - 1;
        if (index >= 0){
            this.mSpeedTypeIndex = index;
        }else{

        }

        this.updateSpeedText();
    },

    getSpeed : function(){
        return this.mSpeed / this.mSpeedTypes[this.mSpeedTypeIndex];
    }
});